
from django.shortcuts import render

# Create your views here.

from login.models import Coach_info
from django.http import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from login.models import Student
from login.models import Subject
from driver_school_after.utils import set_token, get_token, is_blank


# 教练信息
def select_coach(request):
    # 获取所有教练数据
    data = Coach_info.objects.filter().values()
    datalist = list(data)
    list_data = []

    for i in datalist:
        list_data.append(i)

    return JsonResponse({"code": 200, "message": "查询成功", "result": datalist})


# 登录
@api_view(["post"])
def login(request):
    # 获取学员的id,电话，密码
    data = JSONParser().parse(request)
    name = data.get('name')
    password = data.get('password')
    student = Student.objects.filter(name=name, password=password)
    id = student.values("id").first()["id"]
    if student.exists():
        return JsonResponse(
            {"code": 200, "message": "登录成功", "data": student.values().first(), "token": set_token(name, id)})

    return JsonResponse({"code": 400, "message": "账号或者密码错误"})


@api_view(["get"])
def info(request):
    header = request.headers.get("token")
    token = get_token(header)
    print(token)
    id = token["id"]
    print(id)
    return JsonResponse({"code": 200, "data": Student.objects.filter(id=id).values().first()})


# 学员注册
def register(request):
    # 获取学员的信息
    data = JSONParser().parse(request)
    id = str(data.get('id'))
    name = data.get('name')
    age = data.get('age')
    sex = data.get('sex')
    phone = data.get('phone')
    password = data.get('password')
    end_time = data.get('end_time')

    if Student.objects.filter(phone=phone) and Student.objects.filter(password=password):
        return JsonResponse({"code": 200, "message": "登录成功", "token": set_token(phone, id)})
    if is_blank(name) or is_blank(age) or is_blank(sex) or is_blank(phone) or is_blank(password) or is_blank(
            id) or is_blank(end_time):
        return JsonResponse({"code": 500, "message": "必填项不能为空"})
    if Student.objects.filter(phone=phone) or Student.objects.filter(id=id):
        return JsonResponse({"code": 500, "message": "该用户已注册"})

    Student(**data).save()
    return JsonResponse({"code": 500, "message": "注册成功"})


# 考试科目


def subject(request):
    # 获取所有考试科目
    data = Subject.objects.filter().values()
    datalist = list(data)
    list_data = []

    for i in datalist:
        list_data.append(i)

    return JsonResponse({"code": 200, "message": "查询成功", "result": datalist})

from django.shortcuts import render

# Create your views here.
import os
from login.models import Coach_info
from django.http import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from login.models import Student
from login.models import Subject
from driver_school_after.utils import set_token, get_token, is_blank


# 教练信息
def select_coach(request):
    # 获取所有教练数据
    data = Coach_info.objects.filter().values()
    datalist = list(data)
    list_data = []

    for i in datalist:
        list_data.append(i)

    return JsonResponse({"code": 200, "message": "查询成功", "result": datalist})


# 登录
@api_view(["post"])
def login(request):
    # 获取学员的id,电话，密码
    data = JSONParser().parse(request)
    name = data.get('name')
    password = data.get('password')
    student = Student.objects.filter(name=name, password=password)
    id = student.values("id").first()["id"]
    if student.exists():
        return JsonResponse(
            {"code": 200, "message": "登录成功", "data": student.values().first(), "token": set_token(name, id)})

    return JsonResponse({"code": 400, "message": "账号或者密码错误"})


@api_view(["get"])
def info(request):
    header = request.headers.get("token")
    token = get_token(header)
    id = token["id"]
    data = Student.objects.filter(id=id).values().first()
    host = request.get_host()
    print(request.get_port())
    data["image"] = "http://" + host + data["image"]
    return JsonResponse({"code": 200, "data": data})


@api_view(["post"])
def update_img(request):
    header = request.headers.get("token")
    token = get_token(header)
    if token is None:
        return JsonResponse({"status": 201, "message": "请先登录"})
    user_id = token["id"]
    image = request.FILES.get("file").name
    ext = str(image).split(".")[-1]
    imageName = f"user{user_id}.{ext}"
    # 当前脚本的绝对路径
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    # 构造图片保存路径
    image_path = BASE_DIR + "/static/picture/" + imageName
    # 读取图片
    imageData = request.FILES.get("file").read()
    # 写入图片
    student = Student.objects.filter(id=user_id)
    student.update(image="/static/picture/" + imageName)
    with open(image_path, "wb") as f:
        f.write(imageData)
    return JsonResponse({"status": 200, "message": "图片上传成功", "data": student.values().first()})


# 学员注册
def register(request):
    # 获取学员的信息
    data = JSONParser().parse(request)
    id = str(data.get('id'))
    name = data.get('name')
    age = data.get('age')
    sex = data.get('sex')
    phone = data.get('phone')
    password = data.get('password')
    end_time = data.get('end_time')

    if Student.objects.filter(phone=phone) and Student.objects.filter(password=password):
        return JsonResponse({"code": 200, "message": "登录成功", "token": set_token(phone, id)})
    if is_blank(name) or is_blank(age) or is_blank(sex) or is_blank(phone) or is_blank(password) or is_blank(
            id) or is_blank(end_time):
        return JsonResponse({"code": 500, "message": "必填项不能为空"})
    if Student.objects.filter(phone=phone) or Student.objects.filter(id=id):
        return JsonResponse({"code": 500, "message": "该用户已注册"})

    Student(**data).save()
    return JsonResponse({"code": 500, "message": "注册成功"})


# 考试科目


def subject(request):
    # 获取所有考试科目
    data = Subject.objects.filter().values()
    datalist = list(data)
    list_data = []

    for i in datalist:
        list_data.append(i)

    return JsonResponse({"code": 200, "message": "查询成功", "result": datalist})

